import * as stockService from '../services/stockservice';

export default {
    namespace: 'stockrequireapply',
    state:{
        dialogVisiable:false,
        orgList:[],
        applyList:[],//显示
        uomList:[],
        mimList:[],
        deptList:[],
        currData:null
    },

    reducers: {
      'delete'(state, { payload: id }) {
        let newOrgList = state.orgList.filter(item => item.id !== id);
        let newApplyList = state.applyList.filter(item => item.id !== id);
        return {...state,applyList:newApplyList,orgList:newOrgList};
      },
      'create'(state, { payload: item }) {
        let newApplyList = [item,...state.applyList];
        return {...state,applyList:newApplyList,dialogVisiable:false};
      },
      'getList'(state,{payload:applyList}){
        return {...state,applyList:applyList,orgList:applyList}
      },
      'initWidgetData'(state,{payload:initData}){
        const {uomList,mimList,deptList} = initData
        return {...state,uomList,mimList,deptList}
      },
      'trggleDialog'(state,{payload:visiable}){
        if(!visiable){
          return {...state,dialogVisiable:visiable,currData:null}
        }
        return {...state,dialogVisiable:visiable}
      },
      'validDefuncInd'(state,{payload:checked}){
        if(checked){
          let newFialList = state.orgList.filter(item=>item.defuncInd !== 'Y');
          return {...state,applyList:newFialList}
        }else{
          let newFialList = state.orgList;
          return {...state,applyList:newFialList}
        }
      },
      'update'(state,{payload:currData}){
        return {...state,currData:currData,dialogVisiable:true};
      }

    },
    effects: {
        *loadServerData({payload},{ call, put }){
            let data = yield call(stockService.getAllApplies);
            if(data){
                yield put({
                    type:'getList',
                    payload:data.data.applyList
                });
            }
        },

        *loadWidgetData({payload},{ call, put }){
          let data = yield call(stockService.getAllWidgetData);
          if(data){
              yield put({
                  type:'initWidgetData',
                  payload:data.data
              });
          }
        }
    }

  };